package com.itheima.reggie.controller;

import ch.qos.logback.core.joran.util.beans.BeanUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.IUserService;
import com.itheima.reggie.utils.HttpUtils;
import com.itheima.reggie.utils.SMSUtils;
import com.itheima.reggie.utils.SMSUtils2;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private IUserService userService;


    /**
     * 发送验证码
     * @param user
     * @param session
     * @return
     */
    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpSession session){

        //从前端获取用户输入的手机号
        String phone = user.getPhone();
        //随机生成六位数字验证码
        String msg = RandomUtil.randomNumbers(6);
        System.out.println(msg);
        //给手机号发送验证码消息
        //SMSUtils2.sendSms(phone,msg);
        //SMSUtils.sendMessage("阿里云短信测试", "SMS_154950909", "15603948190",msg);
        //传递到浏览器session
        //session.setAttribute("phone",msg);
        redisTemplate.opsForValue().set(phone,msg,10, TimeUnit.MINUTES);
        return R.success("验证码发送成功");
    }

    /**
     * 输入正确的验证码进入页面
     * @param map
     * @param session
     * @return
     */
    @PostMapping("/login")
    public R receiveMsg(@RequestBody Map<String, String> map,HttpSession session){
        String phone = map.get("phone");
        String code = map.get("code");

        //String sCode = session.getAttribute("phone").toString();

        String sCode = (String) redisTemplate.opsForValue().get(phone);

        if(sCode != null && sCode.equals(code)){
            //如果能够比对成功，说明登录成功
            LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(User::getPhone,phone);

            User user = userService.getOne(queryWrapper);
            if(user == null){
                //判断当前手机号对应的用户是否为新用户，如果是新用户就自动完成注册
                user = new User();
                user.setPhone(phone);
                user.setStatus(1);
                userService.save(user);
            }
            session.setAttribute("user",user.getId());
            redisTemplate.delete(phone);
            return R.success(user);
        }
        return R.error("登录失败");
    }

    @PostMapping("/loginout")
    public R logout(HttpSession session){
        session.invalidate();
        return R.success("退出成功");
    }
}
